package com.chidopi.cdf.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chidopi.cdf.util.ModelContextFactory;


public class ItemServiceTag extends SimpleTagSupport {
	private static Logger logger = LoggerFactory.getLogger(ItemServiceTag.class);
	private String itemId;
	private ITagDao tagService;

	public void setItemId(String itemId) {
		this.itemId = itemId;
		logger.debug("ItemID is setted as " + itemId);
	}

	@Override
	public void doTag() throws JspException, IOException {
		try {
			this.tagService = ModelContextFactory.getT("tagService", ITagDao.class);
		} catch (Exception e) {
			logger.warn("load ITagDao failure:", e);
			return ;
		}
		JspWriter out = this.getJspContext().getOut();
		Integer user_id = (Integer) super.getJspContext().getAttribute("user_id", PageContext.SESSION_SCOPE);
		logger.debug("user_id=" + user_id);
		if (null != this.tagService) {
			try {
				String css = this.tagService.getCSS(user_id, this.itemId);
				if (css != null) {
					out.print(css);
				}
			} catch (Exception e) {
				logger.warn("dao error", e);
			}
		} else {
			logger.debug("ITagDao is null");
		}
		logger.debug("doTag OK!!");
	}
}
